package com.demo.mapper;

import com.demo.dto.SysUserDTO;
import com.demo.dto.UserDTO;
import com.demo.entity.SysUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysUserMapper {

    @Select("select sys_user.* " +
            "from sys_user " +
            "where  user_id =#{userId}")
    SysUser getByUserId(String userId);

    @Select("select * from sys_user where login_name=#{loginName} and disabled=0")
    SysUser getByLoginname(String loginName);

    @Insert("INSERT INTO sys_user\n" +
            "(user_id, login_name, login_pass, last_login, telephone, " +
            "user_number, create_time, disabled, role_id, true_name,oicq,mail)\n" +
            "VALUES(#{userId}, #{loginName}, #{loginPass}, #{lastLogin}, #{telephone}," +
            " #{userNumber}, #{createTime}, #{disabled}, #{roleId}, #{trueName},#{oicq},#{mail});")
    int insert(SysUser user);


    @Select("<script>" +
            "select sys_user.*, lab_role.role_name as roleName,sd.dep_name\n" +
            "from sys_user\n" +
            "         left join lab_role on sys_user.role_id = lab_role.role_id\n" +
            "        left join sys_department sd on dep_id=sd.id\n" +
            "order by create_time desc" +
            "</script> ")
    List<SysUser> list();

    @Select("<script>" +
            "select *,lab_role.role_name \n" +
            "from sys_user\n" +
            "         left join lab_role on sys_user.role_id =lab_role.role_id \n" +
            "where sys_user.disabled is not null " +
            "<if test='roleId != null and roleId!=\"\"'>" +
            " and  sys_user.role_id=#{roleId} " +
            "</if>" +
            "<if test='userNumber != null and userNumber!=\"\"'>" +
            " and user_number like '%${userNumber}%' " +
            "</if>" +
            "\n" +
            "order by sys_user.create_time desc" +
            "</script>")
    List<SysUserDTO> listByRole(String roleId, String userNumber);


    @Update("UPDATE sys_user " +
            "SET  telephone=#{telephone}," +
            " user_Number=#{userNumber}," +
            " role_id=#{roleId}, true_name=#{trueName},oicq=#{oicq},mail=#{mail} " +
            " WHERE login_name=#{loginName}")
    int update(SysUser user);

    @Update("UPDATE sys_user set disabled=1 where login_name=#{loginName}")
    int remove(String loginName);

    @Update("delete from sys_user where login_name=#{loginName}")
    int cleanUser(String loginName);

    @Update("UPDATE sys_user SET login_pass=#{loginPass} WHERE login_name=#{loginName}")
    int password(String loginName, String loginPass);

    @Select({"<script>" +
            "select count(*) from sys_user " +
            "<if test='loginName != null and loginName!=\"\"'>" +
            " where login_name=#{loginName}" +
            "</if>" +
            "<if test='telephone != null and telephone!=\"\"'>" +
            " or telephone=#{telephone}" +
            "</if>" +
            "<if test='userNumber != null and userNumber!=\"\"'>" +
            " or user_number=#{userNumber}" +
            "</if>" +
            "</script>"}
    )
    int confirmLoginName(String loginName, String telephone, String userNumber);

    @Select("select sys_user .* from sys_user \n" +
            "where  user_id =#{userId}")
    SysUser findFromUserID(String userId);


    @Update("UPDATE sys_user set disabled =0 WHERE user_id =#{userId}")
    int backUser(String userId);

    @Select("SELECT login_pass FROM sys_user where login_name =#{loginName} ")
    String pass(String loginName);

    @Update("update sys_user set role_id=#{roleId} where user_id=#{userId}")
    int roleId(String userId, String roleId);


    @Select("SELECT user_id,true_name FROM sys_user su where su.role_id=#{roleId} AND su.disabled =0")
    List<UserDTO> findUserIdByRoleId(String roleId);

    @Delete("DELETE FROM student_leader\n" +
            "WHERE user_id =#{userId}")
    int deleteByUserId(String userId);

    @Insert("INSERT INTO student_leader\n" +
            "(user_id, leader_id)\n" +
            "VALUES(#{userId}, #{leaderId});")
    int insertStudentLeader(String userId, String leaderId);

}